package fwx.dao.impl;

import fwx.model.Barber;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.BeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.RowProcessor;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import fwx.dao.BarberDao;
import fwx.util.DataSouceUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BarberImpl implements BarberDao {
    private QueryRunner queryRunner = new QueryRunner(DataSouceUtil.getDataSource());
    private BeanProcessor beanProcessor = new BeanProcessor();
    private RowProcessor processor = new BasicRowProcessor(beanProcessor);
    @Override
    public List<Barber> findAll() {
        String sql = "select * from barber";
        List<Barber> list = new ArrayList<>();
        try {
            list = queryRunner.query(sql, new BeanListHandler<>(Barber.class,processor));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public int deleteBarber(int id) {
        String sql = "delete from barber where id = ?";
        int i = 0;
        try {
            i = queryRunner.update(sql,id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return i;
    }

    @Override
    public int updateBarber(Barber barber) {
        String sql = "update barber set name =?,packages =?,price =?,time =? where id = ?";
        int i = 0;
        Object[] parm = {
               barber.getName(),
               barber.getPackages(),
               barber.getPrice(),
               barber.getTime(),
               barber.getId()
        };
        try {
            i = queryRunner.update(sql,parm);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return i;
    }
    @Override
    public int addBarber(Barber barber) {
        String sql = "insert into barber(name,packages,price,time) values(?,?,?,?)";
        Object[] parm = {
                barber.getName(),
                barber.getPackages(),
                barber.getPrice(),
                barber.getTime()
        };
        int ret = 0;
        try {
            ret = queryRunner.update(sql,parm);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return ret;
    }

    @Override
    public List<Barber> findBarberByName(String name) {
        String sql = "select * from barber where name =?";
        List<Barber> list = new ArrayList<>();
        try {
            list = queryRunner.query(sql, new BeanListHandler<>(Barber.class,processor),name);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public Barber finById(int id) {
        String sql = "select * from barber where id =?";
        Barber barber = null;
        try {
            barber = queryRunner.query(sql,new BeanHandler<>(Barber.class),id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return barber;
    }

    @Override
    public int totalRecord() {
        String sql = "select count(*) from barber";
        Long sum = null;
        try {
            sum = queryRunner.query(sql,new ScalarHandler<>());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return sum.intValue();
    }
    @Override
    //分页查询
    public  List<Barber> findListByBarber(int from, int pageSize) {
        String sql = "select * from barber order by id desc limit ? , ?";
        List<Barber> list = new ArrayList<>();
        Object[] parm = {
                from,
                pageSize
        };
        try {
            list = queryRunner.query(sql, new BeanListHandler<>(Barber.class),parm);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }
}
